//
//  STMenuControl.m
//  blanket
//
//  Created by Mac on 2017/12/5.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STMenuControl.h"

@implementation STMenuControl

- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)titleColor{
    if (self == [super initWithFrame:frame]) {
        
//        self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 44, 44)];
//        self.imageButton.centerX = self.width / 2;
//        [self.imageButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//        self.imageButton.userInteractionEnabled = NO;
//        [self addSubview:self.imageButton];
        __weak typeof(self) weakSelf =  self;
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)
                                                         title:title
                                                    titleColor:titleColor
                                                     titleFont:14
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:imageName]];
        [buyButton setClicAction:^(UIButton *sender) {
            [weakSelf controlDidSelcted:weakSelf];
        }];
        CGFloat titleWitdh = [title st_widthWithheight:25 font:14];
        if (titleWitdh > 40) {
            titleWitdh = titleWitdh - 7;
        }
        CGSize imageSize = buyButton.imageView.size;
        buyButton.imageEdgeInsets = UIEdgeInsetsMake(-30, titleWitdh, 0, 0);
        buyButton.titleEdgeInsets = UIEdgeInsetsMake(30, - imageSize.width, 0, 0);
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [buyButton setTitleColor:TM_redColor forState:UIControlStateHighlighted];
        self.titleButton = buyButton;
        [self addSubview:buyButton];
        [self addTarget:self action:@selector(controlDidSelcted:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)controlDidSelcted:(STMenuControl*)sender{
    if (self.onSelctedControl) {
        self.onSelctedControl(sender);
    }
}
@end
